﻿using System.ComponentModel;
using AgFx;
using DepSoft.Mobile.WP7.FollwIt.Framework;
using DepSoft.Mobile.WP7.FollwIt.Messages;
using DepSoft.Mobile.WP7.FollwIt.Model;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using WP7Contrib.Services.Navigation;
using WP7Contrib.Services.Settings;
using WP7Contrib.Services.Storage;
using System.Windows;
using System;

namespace DepSoft.Mobile.WP7.FollwIt.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class UserProfileViewModel : ViewModelBaseWp7
    {
        #region Public properties
        public string ApplicationTitle
        {
            get
            {
                //Use of the Coding 4 FUn PhoneHelper to get the application title from the configuration
                //return PhoneHelper.GetAppAttribute("Title");
                return string.Concat("PROFILE ", FollwItApi.GetInstance().UserName.ToUpper());
            }
        }

        public string PageName
        {
            get
            {
                return "friends";
            }
        }

        public RelayCommand<CancelEventArgs> BackKeyPressCommand
        {
            get;
            private set;
        }

        public RelayCommand<string> SelectedFriendCommand
        {
            get;
            private set;
        }
        #endregion

        #region Public bindable properties
        #region Friends
        /// <summary>
        /// The <see cref="Friends" /> property's name.
        /// </summary>
        public const string FriendsPropertyName = "Friends";

        private agfxFriendsViewModel _friends = null;

        /// <summary>
        /// Gets the MovieCollection property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public agfxFriendsViewModel Friends
        {
            get
            {
                return _friends;
            }

            set
            {
                this.SetPropertyAndNotify(ref _friends, value, FriendsPropertyName);
            }
        }
        #endregion

        #region ProgressIndicatorVisible
        /// <summary>
        /// The <see cref="ProgressIndicatorVisible" /> property's name.
        /// </summary>
        public const string ProgressIndicatorVisiblePropertyName = "ProgressIndicatorVisible";

        private bool _progressIndicatorVisible = false;

        /// <summary>
        /// Gets the ProgressIndicatorVisible property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public bool ProgressIndicatorVisible
        {
            get
            {
                return _progressIndicatorVisible;
            }

            set
            {
                this.SetPropertyAndNotify(ref _progressIndicatorVisible, value, ProgressIndicatorVisiblePropertyName);
            }
        }
        #endregion
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the UserProfileViewModel1 class.
        /// </summary>
        public UserProfileViewModel(INavigationService navigationService, IStorageService storageService, IStoreSettings settingsService, IMessenger messenger)
            : base(navigationService, storageService, settingsService, messenger)
        {
            this.BackKeyPressCommand = new RelayCommand<CancelEventArgs>((args) => this.BackKeyPressed(args));
            this.SelectedFriendCommand = new RelayCommand<string>((friendUserName) => this.SelectedFriend(friendUserName));

            this.MessengerInstance.Register<NotificationMessage>(this, action =>
                {
                    switch (action.Notification)
                    {
                        case "AuthenticateComplete":
                            this.Load();
                            break;
                        default:
                            break;
                    }
                });

            if (string.IsNullOrEmpty(FollwItApi.GetInstance().UserName) || string.IsNullOrEmpty(FollwItApi.GetInstance().Password))
            {
                MessageBoxResult result =
                    MessageBox.Show(
                        "No user information found!" + Environment.NewLine + "Please provide it on the Settings page...",
                        "Info required", MessageBoxButton.OK);
            }
            else
                this.Load();
        }

        #endregion

        #region Private methods
        private void Load()
        {
            this.ProgressIndicatorVisible = true;
            this.Friends = DataManager.Current.Load<agfxFriendsViewModel>(FollwItApi.GetInstance().UserName, (vm) =>
            {
                this.ProgressIndicatorVisible = false;
            },
                (ex) =>
                {
                    this.ProgressIndicatorVisible = false;
                    throw new FollwitException("Error while loading Friends", ex);
                });
        }

        private void BackKeyPressed(CancelEventArgs args)
        {
            //this.SelectedFriend(FollwItApi.GetInstance().UserName, true);
        }

        private void SelectedFriend(string friendUserName, bool goBack = false)
        {
            this.MessengerInstance.Send<SelectedFriendChangedMessage>(new SelectedFriendChangedMessage(friendUserName));
            //if(goBack)
            //    NavigationService.GoBack();
            //else
                NavigationService.Navigate(ViewModelLocator.CollectionPageUri);
        }
        #endregion

        #region ViewModelBaseWp7 implementation
        protected override void IsBeingActivated(WP7Contrib.Services.Storage.IStorage storage)
        {
            //throw new System.NotImplementedException();
        }

        protected override void IsBeingDeactivated(WP7Contrib.Services.Storage.IStorage storage)
        {
            //throw new System.NotImplementedException();
        }
        #endregion
    }
}